 module Admin::BaseHelper

    #所有产品顶级分类下拉
    def category_all_parent
      Category.roots.map{|c|[c.name,c.id]}
    end

    # 订单状态下拉数据
    def order_status_all
      OrderStatus.all.map{|o|[o.name,o.id]}
    end

    # 支付方式下拉数据
    def payment_all
      Payment.all.map{|p| [p.name,p.id]}
    end

    # 发送时间下拉数据
    def delivery_schedule_date_all
      DeliverySchedule.all.map{|d|[d.name,d.id]}
    end


    # 邮送方式下拉数握
    def mailing_all
      Mailing.all.map{|m|[m.name,m.id]}
    end
    
#    def page_title_tag
#      @page_title || 'Admin / ' + controller_name
#    end
    
    def title(text, options = {})
      content_for(:title) { text }
    end
    
    def backward_button_tag(path= nil,text = nil, options = {})
      button_link_tag(
        text || t("button.back"),
        (url_for(path = (path ||= {:action=>"index"} )) rescue url_for(:back)),
        options.reverse_merge(:id => 'backward-button', :class => 'button')
      )
    end
    
    # Generate a hyper link, clicking on which will popup a modal dialog.
    def link_to_dialog(text, options, html_options = {})
      link_to(
        text,
        options.reverse_merge(:width => 650, :height => 500, "TB_iframe" => "true"),
        html_options.reverse_merge(:class => "thickbox", :title => text)
      )
    end
    
    def link_to_window(text, options, html_options = {})
      link_to(
        text,
        options,
        html_options.merge(:onclick => "return ReferenceWindow.open(this.href);")
      )
    end
    
    # Date range select using Calender Date Select
    def date_range_select_tag(name_prefix, field, options = {})
      html = []
      [:start, :end].each do |part|
        html << calendar_date_select_tag(
          "#{name_prefix}[#{field}][#{part}]",
          (options[name_prefix][field][part] rescue ''),
          :size => 10
        )
      end
      
      return html.join(' ~ ')
    end
    
    # Definition for styling the <code>textarea</code> tag.
    def text_area_options
      { :cols => 30, :rows => 3 }
    end
    
    # Popup a module window to pickup/browser related records. The callback
    # will be fired after closing the dialog window.
    # For more information, check out the "ReferenceWindow" object in "admin.js"
    def button_to_reference_window(url, javascript, options ={})
      link_to_function 'pickup', javascript, options
    end

    def order_status_select_tag(order)
      return select_tag(
        "order[status_id]",
        options_for_select(
         [""] + OrderStatus.all.map{|o|[o.name,o.id]}, order.status_id
        ),
        :onchange => remote_function(
          :url => admin_order_path(order),
          :method => :put,
          :submit => "order_status_#{order.id}",
          :before => "$('loader_#{order.id}').show();disabled_page('true');",
          :complete => "$('loader_#{order.id}').hide();disabled_page('false');"
        )
      )
    end
  end
